from urllib.request import Request

from rest_framework.fields import empty
from rest_framework.serializers import ModelSerializer


class CustomModelSerializer(ModelSerializer):
    def __init__(self, instance=None, data=empty, request=None, **kwargs):
        super().__init__(instance, data, **kwargs)
        self.request: Request = request

    def save(self, **kwargs):
        return super().save(**kwargs)

    def create(self, validated_data):
        # if self.context.get("request"):
        #     self.request = self.context.get("request")
        # if self.request:
        #     username = self.get_request_username()
        #     if self.modifier_field_name in self.fields.fields:
        #         validated_data[self.modifier_field_name] = username
        #     if self.creator_field_name in self.fields.fields:
        #         validated_data[self.creator_field_name] = self.request.user
        #     if self.dept_belong_id_field_name in self.fields.fields:
        #         validated_data[self.dept_belong_id_field_name] = getattr(self.request.user, "dept_id", None)
        return super().create(validated_data)

    def update(self, instance, validated_data):
        # if self.request:
        #     if hasattr(self.instance, self.modifier_field_name):
        #         self.instance.modifier = self.get_request_username()
        return super().update(instance, validated_data)
